package authentication;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class DatabaseAuthentication
{
	public static void main(String[] args)
	{
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
		}
		catch (ClassNotFoundException e) { e.printStackTrace(); }
		
		java.sql.Connection dbConnection = null;
		
		try
		{
			dbConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/Capstone", "root", "password");
		}
		catch (SQLException e) { e.printStackTrace(); }
		
		if (dbConnection != null)
		{
			Scanner reader = new Scanner(System.in);
			
			String username = reader.nextLine();
			String password = reader.nextLine();
			
			try
			{
				Statement statement = dbConnection.createStatement();
				
				statement.execute("SELECT COUNT(*) FROM users WHERE username='" + username + "' AND password='" + password + "'");
				
				ResultSet results = statement.getResultSet();
				
				results.next();
				
				if (results.getInt(1) != 0)
					System.out.println("Success");
				else
					System.out.println("Fail");
				
				dbConnection.close();
			}
			catch (SQLException e) { e.printStackTrace(); }
		}
	}
}
